﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models.Users;
using Think.Lenovo.Com.Common;
namespace Think.Lenovo.Com.DataAccess.User
{
    public class User
    {
        /// <summary>
        /// 添加一个用户
        /// </summary>
        /// <param name="model">用户实体类</param>
        /// <returns>自增id</returns>
        public long AddUser(UserModel model)
        {
            try
            {
                String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

                //sql语句
                StringBuilder sql = new StringBuilder(128);
                sql.Append("insert into ")
                    .Append("tbl_user (userName,userPassword,userRole,userLevelId,userState)")
                    .Append("values(@userName,md5(@userPassword),@userRole,@userLevelId,@userState)");


                MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@userName",MySqlDbType.VarChar,50),
                    new MySqlParameter("@userPassword",MySqlDbType.VarChar,32),
                    new MySqlParameter("@userRole",MySqlDbType.Int32),
                    new MySqlParameter("@userLevelId",MySqlDbType.Int32),
                    new MySqlParameter("@userState",MySqlDbType.Int32)
                };

                ps[0].Value = model.UserName;
                ps[1].Value = model.UserPassword;
                ps[2].Value = model.UserRoleId;
                ps[3].Value = model.UserLevelId;
                ps[4].Value = model.UserStateId;

                long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
                return id;
            }
            catch (Exception e)
            {
                String LogRoot = System.Configuration.ConfigurationManager.AppSettings["LogRoot"];
                Log.RecordLog(LogRoot, e);
                return -1;
            }
        }
        /// <summary>
        /// 修改一个用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Update(UserModel model)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            //Sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Update  ")
                .Append("tbl_user Set userName=@userName,")
                .Append("userPassword=@userPassword,userRole=@userRole,")
                .Append("userLevelId=@userLevelId,userState=@userState")
                .Append(" Where Id=@Id");

            MySqlParameter[] ps = new MySqlParameter[] {
                new MySqlParameter("@userName",MySqlDbType.VarChar,50),
                new MySqlParameter("@userPassword",MySqlDbType.VarChar,32),
                new MySqlParameter("@userRole",MySqlDbType.Int32),
                new MySqlParameter("@userLevelId",MySqlDbType.Int32),
                new MySqlParameter("@userState",MySqlDbType.Int32),
                new MySqlParameter("@Id",MySqlDbType.Int32)
            };

            ps[0].Value = model.UserName;
            ps[1].Value = Encrypt.Md5(model.UserPassword, System.Text.Encoding.UTF8, false);
            ps[2].Value = model.UserRoleId;
            ps[3].Value = model.UserLevelId;
            ps[4].Value = model.UserStateId;
            ps[5].Value = model.Id;

            return MysqlHelper.Update(conn, sql.ToString(), ps);

        }

        public long Update(UserModel model,bool isUpdatePassword)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            //Sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Update  ")
                .Append("tbl_user Set userName=@userName,")
                .Append("userPassword=@userPassword,userRole=@userRole,")
                .Append("userLevelId=@userLevelId,userState=@userState")
                .Append(" Where Id=@Id");

            MySqlParameter[] ps = new MySqlParameter[] {
                new MySqlParameter("@userName",MySqlDbType.VarChar,50),
                new MySqlParameter("@userPassword",MySqlDbType.VarChar,32),
                new MySqlParameter("@userRole",MySqlDbType.Int32),
                new MySqlParameter("@userLevelId",MySqlDbType.Int32),
                new MySqlParameter("@userState",MySqlDbType.Int32),
                new MySqlParameter("@Id",MySqlDbType.Int32)
            };

            ps[0].Value = model.UserName;
            if (isUpdatePassword)
            {
                ps[1].Value = Encrypt.Md5(model.UserPassword, System.Text.Encoding.UTF8, false);
            }
            else
            {
                ps[1].Value = model.UserPassword;
            }
            ps[2].Value = model.UserRoleId;
            ps[3].Value = model.UserLevelId;
            ps[4].Value = model.UserStateId;
            ps[5].Value = model.Id;

            return MysqlHelper.Update(conn, sql.ToString(), ps);

        }

        /// <summary>
        /// 更新用户状态
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="stateId">状态id</param>
        /// <returns>受景响的行数</returns>
        public long Update(int userId, int stateId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            //Sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Update  ")
                .Append("tbl_user Set userState=@userState")
                .Append(" Where Id=@Id");

            MySqlParameter[] ps = new MySqlParameter[] {
                new MySqlParameter("@userState",MySqlDbType.Int32),
                new MySqlParameter("@Id",MySqlDbType.Int32)
            };

            ps[0].Value = stateId;
            ps[1].Value = userId;

            return MysqlHelper.Update(conn, sql.ToString(), ps);
        }

        public void Update(int[] userId, int stateId)
        {
            foreach (int i in userId)
            {
                Update(i, stateId);
            }
        }

        /// <summary>
        /// 获得一些用户实体
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数据数量</param>
        /// <returns></UserModel></returns>
        public LinkedList<UserModel> GetUsers(int pageIndex, int pageSize)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_user limit ")
                .Append((pageIndex - 1) * pageSize)
                .Append(",")
                .Append(pageSize);
            DataSet ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            LinkedList<UserModel> list = new LinkedList<UserModel>();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        UserModel model = new UserModel();
                        model.Id = Convert.ToInt32(row["Id"]);
                        model.UserName = Convert.ToString(row["UserName"]);
                        model.UserPassword = Convert.ToString(row["userPassword"]);
                        model.UserRoleId = Convert.ToInt32(row["userRole"]);
                        model.UserLevelId = Convert.ToInt32(row["userLevelId"]);
                        model.UserStateId = Convert.ToInt32(row["userState"]);
                        list.AddLast(model);
                    }
                }
            }
            return list;
        }

        public DataSet GetUsers(int top)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_user limit ")
                .Append("0,")
                .Append(top.ToString());
            DataSet ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            return ds;
        }

        /// <summary>
        /// 获得一个UserModel对象,并且该对象的状态为"正常"
        /// </summary>
        /// <param name="key">用户名称或者用户公司email 或者统一的用户名</param>
        /// <param name="password">用户密码,密码明文</param>
        /// <returns></returns>
        public ViewUserModel GetUser(String key, String password)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Select * From View_User Where ")
                .Append("(userName=@userName or companyEmail=@userName or formatedUserName=@userName)")
                .Append(" And userPassword=md5(@userPassword) And UserStateId=1");
            MySqlParameter[] ps = new MySqlParameter[] { 
                new MySqlParameter("@userName",MySqlDbType.VarChar,200),
                new MySqlParameter("@userPassword",MySqlDbType.VarChar,200)
            };
            ps[0].Value = key;
            ps[1].Value = password;

            DataSet ds = MysqlHelper.Query(conn, sql.ToString(), ps);
            ViewUserModel model = new ViewUserModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DateTime now = DateTime.Now;
                        int i = 0;
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.UserName = Convert.ToString(ds.Tables[0].Rows[0]["userName"]);
                        model.UserPassword = Convert.ToString(ds.Tables[0].Rows[0]["userPassword"]);
                        model.UserRoleId = Convert.ToInt32(ds.Tables[0].Rows[0]["userRoleId"]);
                        model.UserRoleName = Convert.ToString(ds.Tables[0].Rows[0]["userRoleName"]);
                        model.UserLevelId = Convert.ToInt32(ds.Tables[0].Rows[0]["userLevelId"]);
                        model.UserLevelName = Convert.ToString(ds.Tables[0].Rows[0]["userLevelName"]);
                        model.UserStateId = Convert.ToInt32(ds.Tables[0].Rows[0]["userStateId"]);
                        model.UserStateName = Convert.ToString(ds.Tables[0].Rows[0]["userStateName"]);
                        model.CompanyName = Convert.ToString(ds.Tables[0].Rows[0]["companyName"]);
                        model.RealName = Convert.ToString(ds.Tables[0].Rows[0]["realName"]);

                        DateTime.TryParse(ds.Tables[0].Rows[0]["companyRegisteDate"].ToString(), out now);
                        model.CompanyRegisteDate = now;
                        model.CompanyAddress = Convert.ToString(ds.Tables[0].Rows[0]["companyAddress"]);
                        model.UserEmail = Convert.ToString(ds.Tables[0].Rows[0]["userEmail"]);
                        model.CompanyEmail = Convert.ToString(ds.Tables[0].Rows[0]["companyEmail"]);
                        model.CompanyTel = Convert.ToString(ds.Tables[0].Rows[0]["companyTel"]);
                        model.UserPhoneNumber = Convert.ToString(ds.Tables[0].Rows[0]["userPhoneNumber"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyRegion"].ToString(), out i);
                        model.CompanyRegion = i;
                        model.CompanyRegionName = Convert.ToString(ds.Tables[0].Rows[0]["companyRegionName"]);
                        model.RegionUnits = Convert.ToString(ds.Tables[0].Rows[0]["units"]);
                        model.FormatedUserName = Convert.ToString(ds.Tables[0].Rows[0]["formatedUserName"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["CompanyGrade"].ToString(), out i);
                        model.CompanyGrade = i;

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyTypeId"].ToString(), out i);
                        model.CompanyTypeId = i;

                        model.CompanyTypeName = Convert.ToString(ds.Tables[0].Rows[0]["companyTypeName"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyPropertyId"].ToString(), out i);
                        model.CompanyPropertyId = i;

                        model.CompanyPropertyName = Convert.ToString(ds.Tables[0].Rows[0]["companyPropertyName"]);
                        model.CompanyPostalCode = Convert.ToString(ds.Tables[0].Rows[0]["companyPostalCode"]);
                        model.CompanyForward = Convert.ToString(ds.Tables[0].Rows[0]["forward"]);
                        model.CompanyFax = Convert.ToString(ds.Tables[0].Rows[0]["companyFax"]);
                        model.CompanyBusinessNumber = Convert.ToString(ds.Tables[0].Rows[0]["companyBusinessNumber"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyRegisteFund"].ToString(), out i);
                        model.CompanyRegisteFund = i;

                        RegionInfo regionInfo = new RegionInfo();
                        #region//得到当用户所属区域名称
                        model.CompanyRegionName = regionInfo.GetModel(model.CompanyRegion).NodeName;
                        #endregion
                        #region//得到当前用户所属省的名称
                        model.CompanyProvinceName = regionInfo.GetModel(model.CompanyProvinceId).NodeName;
                        #endregion
                        #region//得到当前用户所属城市名称
                        model.CompanyCityName = regionInfo.GetModel(model.CompanyCityId).NodeName;
                        #endregion
                    }
                }
            }
            return model;
        }

        public ViewUserModel GetUser(String key)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Select * From View_User Where ")
                .Append("(userName=@userName or companyEmail=@userName or formatedUserName=@userName)");
            MySqlParameter[] ps = new MySqlParameter[] { 
                new MySqlParameter("@userName",MySqlDbType.VarChar,200)
            };
            ps[0].Value = key;

            DataSet ds = MysqlHelper.Query(conn, sql.ToString(), ps);
            ViewUserModel model = new ViewUserModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DateTime now = DateTime.Now;
                        int i = 0;
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.UserName = Convert.ToString(ds.Tables[0].Rows[0]["userName"]);
                        model.UserPassword = Convert.ToString(ds.Tables[0].Rows[0]["userPassword"]);
                        model.UserRoleId = Convert.ToInt32(ds.Tables[0].Rows[0]["userRoleId"]);
                        model.UserRoleName = Convert.ToString(ds.Tables[0].Rows[0]["userRoleName"]);
                        model.UserLevelId = Convert.ToInt32(ds.Tables[0].Rows[0]["userLevelId"]);
                        model.UserLevelName = Convert.ToString(ds.Tables[0].Rows[0]["userLevelName"]);
                        model.UserStateId = Convert.ToInt32(ds.Tables[0].Rows[0]["userStateId"]);
                        model.UserStateName = Convert.ToString(ds.Tables[0].Rows[0]["userStateName"]);
                        model.CompanyName = Convert.ToString(ds.Tables[0].Rows[0]["companyName"]);
                        model.RealName = Convert.ToString(ds.Tables[0].Rows[0]["realName"]);

                        DateTime.TryParse(ds.Tables[0].Rows[0]["companyRegisteDate"].ToString(), out now);
                        model.CompanyRegisteDate = now;
                        model.CompanyAddress = Convert.ToString(ds.Tables[0].Rows[0]["companyAddress"]);
                        model.UserEmail = Convert.ToString(ds.Tables[0].Rows[0]["userEmail"]);
                        model.CompanyEmail = Convert.ToString(ds.Tables[0].Rows[0]["companyEmail"]);
                        model.CompanyTel = Convert.ToString(ds.Tables[0].Rows[0]["companyTel"]);
                        model.UserPhoneNumber = Convert.ToString(ds.Tables[0].Rows[0]["userPhoneNumber"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyRegion"].ToString(), out i);
                        model.CompanyRegion = i;
                        model.CompanyRegionName = Convert.ToString(ds.Tables[0].Rows[0]["companyRegionName"]);
                        model.RegionUnits = Convert.ToString(ds.Tables[0].Rows[0]["units"]);
                        model.FormatedUserName = Convert.ToString(ds.Tables[0].Rows[0]["formatedUserName"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["CompanyGrade"].ToString(), out i);
                        model.CompanyGrade = i;

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyTypeId"].ToString(), out i);
                        model.CompanyTypeId = i;

                        model.CompanyTypeName = Convert.ToString(ds.Tables[0].Rows[0]["companyTypeName"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyPropertyId"].ToString(), out i);
                        model.CompanyPropertyId = i;

                        model.CompanyPropertyName = Convert.ToString(ds.Tables[0].Rows[0]["companyPropertyName"]);
                        model.CompanyPostalCode = Convert.ToString(ds.Tables[0].Rows[0]["companyPostalCode"]);
                        model.CompanyForward = Convert.ToString(ds.Tables[0].Rows[0]["forward"]);
                        model.CompanyFax = Convert.ToString(ds.Tables[0].Rows[0]["companyFax"]);
                        model.CompanyBusinessNumber = Convert.ToString(ds.Tables[0].Rows[0]["companyBusinessNumber"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyRegisteFund"].ToString(), out i);
                        model.CompanyRegisteFund = i;

                        RegionInfo regionInfo = new RegionInfo();
                        #region//得到当用户所属区域名称
                        model.CompanyRegionName = regionInfo.GetModel(model.CompanyRegion).NodeName;
                        #endregion
                        #region//得到当前用户所属省的名称
                        model.CompanyProvinceName = regionInfo.GetModel(model.CompanyProvinceId).NodeName;
                        #endregion
                        #region//得到当前用户所属城市名称
                        model.CompanyCityName = regionInfo.GetModel(model.CompanyCityId).NodeName;
                        #endregion
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得一个UserModel对象,并且该用户的状态为任意状态
        /// </summary>
        /// <param name="key">用户名、邮箱、统一用户名</param>
        /// <param name="password">密码</param>
        /// <param name="isPlaintext">密码是否是明文</param>
        /// <returns></returns>
        public ViewUserModel GetUser(String key, String password, bool isPlaintext)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Select * From View_User Where ");
            sql.Append(" (userName=@userName or companyEmail=@userName or formatedUserName=@userName) ");
            if (isPlaintext)
            {
                sql.Append(" And userPassword=md5(@userPassword)");
            }
            else
            {
                sql.Append(" And userPassword=@userPassword");     
            }
            MySqlParameter[] ps = new MySqlParameter[] { 
                new MySqlParameter("@userName",MySqlDbType.VarChar,200),
                new MySqlParameter("@userPassword",MySqlDbType.VarChar,200)
            };
            ps[0].Value = key;
            ps[1].Value = password;

            DataSet ds = MysqlHelper.Query(conn, sql.ToString(), ps);
            ViewUserModel model = new ViewUserModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DateTime now = DateTime.Now;
                        int i = 0;
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.UserName = Convert.ToString(ds.Tables[0].Rows[0]["userName"]);
                        model.UserPassword = Convert.ToString(ds.Tables[0].Rows[0]["userPassword"]);
                        model.UserRoleId = Convert.ToInt32(ds.Tables[0].Rows[0]["userRoleId"]);
                        model.UserRoleName = Convert.ToString(ds.Tables[0].Rows[0]["userRoleName"]);
                        model.UserLevelId = Convert.ToInt32(ds.Tables[0].Rows[0]["userLevelId"]);
                        model.UserLevelName = Convert.ToString(ds.Tables[0].Rows[0]["userLevelName"]);
                        model.UserStateId = Convert.ToInt32(ds.Tables[0].Rows[0]["userStateId"]);
                        model.UserStateName = Convert.ToString(ds.Tables[0].Rows[0]["userStateName"]);
                        model.CompanyName = Convert.ToString(ds.Tables[0].Rows[0]["companyName"]);
                        model.RealName = Convert.ToString(ds.Tables[0].Rows[0]["realName"]);

                        DateTime.TryParse(ds.Tables[0].Rows[0]["companyRegisteDate"].ToString(), out now);
                        model.CompanyRegisteDate = now;
                        model.CompanyAddress = Convert.ToString(ds.Tables[0].Rows[0]["companyAddress"]);
                        model.UserEmail = Convert.ToString(ds.Tables[0].Rows[0]["userEmail"]);
                        model.CompanyEmail = Convert.ToString(ds.Tables[0].Rows[0]["companyEmail"]);
                        model.CompanyTel = Convert.ToString(ds.Tables[0].Rows[0]["companyTel"]);
                        model.UserPhoneNumber = Convert.ToString(ds.Tables[0].Rows[0]["userPhoneNumber"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyRegion"].ToString(), out i);
                        model.CompanyRegion = i;
                        model.CompanyRegionName = Convert.ToString(ds.Tables[0].Rows[0]["companyRegionName"]);
                        model.RegionUnits = Convert.ToString(ds.Tables[0].Rows[0]["units"]);
                        model.FormatedUserName = Convert.ToString(ds.Tables[0].Rows[0]["formatedUserName"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["CompanyGrade"].ToString(), out i);
                        model.CompanyGrade = i;

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyTypeId"].ToString(), out i);
                        model.CompanyTypeId = i;

                        model.CompanyTypeName = Convert.ToString(ds.Tables[0].Rows[0]["companyTypeName"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyPropertyId"].ToString(), out i);
                        model.CompanyPropertyId = i;

                        model.CompanyPropertyName = Convert.ToString(ds.Tables[0].Rows[0]["companyPropertyName"]);
                        model.CompanyPostalCode = Convert.ToString(ds.Tables[0].Rows[0]["companyPostalCode"]);
                        model.CompanyForward = Convert.ToString(ds.Tables[0].Rows[0]["forward"]);
                        model.CompanyFax = Convert.ToString(ds.Tables[0].Rows[0]["companyFax"]);
                        model.CompanyBusinessNumber = Convert.ToString(ds.Tables[0].Rows[0]["companyBusinessNumber"]);

                        Int32.TryParse(ds.Tables[0].Rows[0]["companyRegisteFund"].ToString(), out i);
                        model.CompanyRegisteFund = i;

                        RegionInfo regionInfo = new RegionInfo();
                        #region//得到当用户所属区域名称
                        model.CompanyRegionName = regionInfo.GetModel(model.CompanyRegion).NodeName;
                        #endregion
                        #region//得到当前用户所属省的名称
                        model.CompanyProvinceName = regionInfo.GetModel(model.CompanyProvinceId).NodeName;
                        #endregion
                        #region//得到当前用户所属城市名称
                        model.CompanyCityName = regionInfo.GetModel(model.CompanyCityId).NodeName;
                        #endregion
                    }
                }
            }
            return model;
        }
        /// <summary>
        /// 根据id获得一个用户实体
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns>返回一个用户实体</returns>
        public UserModel GetUser(int id)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(128);
            sql.Append("Select * From View_User Where ")
                .Append("Id = @Id");
            MySqlParameter[] ps = new MySqlParameter[] { 
                new MySqlParameter("@Id",MySqlDbType.Int32)
            };
            ps[0].Value = id;

            DataSet ds = MysqlHelper.Query(conn, sql.ToString(), ps);
            UserModel model = new UserModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.UserName = Convert.ToString(ds.Tables[0].Rows[0]["userName"]);
                        model.UserPassword = Convert.ToString(ds.Tables[0].Rows[0]["userPassword"]);
                        model.UserRoleId = Convert.ToInt32(ds.Tables[0].Rows[0]["userRoleId"]);
                        model.UserLevelId = Convert.ToInt32(ds.Tables[0].Rows[0]["userLevelId"]);
                        model.UserStateId = Convert.ToInt32(ds.Tables[0].Rows[0]["userStateId"]);
                    }
                }
            }
            return model;
        }
    }
}
